package me.shurufa.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.shurufa.R;
import me.shurufa.activities.NoticeMessageReplyActivity;
import me.shurufa.bean.Notice;
import me.shurufa.holder.BaseViewHolder;
import me.shurufa.utils.Utils;

/**
 * 消息通知列表 适配器
 */
public class NoticeMessageAdapter extends RecyclerView.Adapter<NoticeMessageAdapter.NoticeMessageHolder> {

    private static final int TYPE_NORMAL = 0;
    private static final int TYPE_SYSTEM = 1;

    private Context mContext;
    private List<Notice> dataList;

    public NoticeMessageAdapter(Context context, List<Notice> dataList) {
        this.mContext = context;
        this.dataList = dataList;
    }

    @Override
    public NoticeMessageHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (TYPE_NORMAL == viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_notice_message, parent, false);
            NoticeMessageHolder holder = new NoticeMessageHolder(v, parent);
            return holder;
        }

        if (TYPE_SYSTEM == viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_notice_message_system, parent, false);
            NoticeMessageHolder holder = new NoticeMessageHolder(v, parent);
            return holder;
        }


        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_notice_message, parent, false);
        NoticeMessageHolder holder = new NoticeMessageHolder(v, parent);
        return holder;
    }

    @Override
    public void onBindViewHolder(NoticeMessageHolder holder, int position) {
        Notice notice = dataList.get(position);
        String itemContent = notice.f_name + " " + notice.msg + " “" + notice.content + "”";
        holder.tv_content.setText(itemContent);

        initListener(holder, position);
    }

    private void initListener(NoticeMessageHolder holder, int position) {
        final Notice notice = dataList.get(position);
        holder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent in = new Intent(mContext, NoticeMessageReplyActivity.class);
                NoticeMessageReplyActivity.initArguments(in, notice);
                mContext.startActivity(in);
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        // return position != dataList.size() - 1 ? TYPE_NORMAL : TYPE_SYSTEM;
        return TYPE_NORMAL;
    }

    /**
     * 添加数据在尾部
     * @param items datalist
     */
    public void append(List<Notice> items) {
        if (Utils.isNullForList(items)) {
            return;
        }
        int pos = dataList.size();
        dataList.addAll(items);
        notifyItemRangeInserted(pos, items.size());
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }

    public void remove(int position) {
        if (dataList.size() > 0) {
            dataList.remove(position);
            this.notifyItemRemoved(position);
        }
    }

    public void clear() {
        int size = dataList.size();
        dataList.clear();
        this.notifyItemRangeRemoved(0, size);
    }


    // ViewHolder
    public static class NoticeMessageHolder extends BaseViewHolder {

        @Bind(R.id.item)
        View item;

        @Bind(R.id.tv_content)
        TextView tv_content;

        public NoticeMessageHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public NoticeMessageHolder(View itemView, Object parent) {
            super(itemView, parent);
            ButterKnife.bind(this, itemView);
        }

    }
}
